package com.cxk.hr.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxk.hr.pojo.Attendance;
import com.cxk.hr.pojo.Department;
import com.cxk.hr.pojo.Employee;
import com.cxk.hr.pojo.Overtime;
import com.cxk.hr.service.AttendanceService;
import com.cxk.hr.service.EmployeeService;
import com.cxk.hr.service.impl.AttendanceServiceImpl;
import com.cxk.hr.service.impl.EmployeeServiceImpl;

@Controller
@RequestMapping("attendance")
public class AttendanceController {
	@Autowired
	private AttendanceService attendanceService = new AttendanceServiceImpl();
	@Autowired
	private EmployeeService employeeService = new EmployeeServiceImpl();

	/**
	 * 个人考勤
	 * 
	 * @param id
	 * @param model
	 */
	@RequestMapping("/oneself/{id}/Attend")
	public String attend(@PathVariable Integer id, Model model) {
		Employee employee = (Employee) employeeService.findEmpById(id);
		List<Attendance> att = employeeService.findAllatt(employee.getId());
		model.addAttribute("att", att);
		System.out.println(att);
		return "oneself_attendance";
	}

	/**
	 * 主页
	 *
	 */
	@RequestMapping("welcome")
	public String welcome() {
		return "welcome";
	}

	/**
	 * 上班签到
	 * 
	 * @param num
	 * @return
	 */
	@RequestMapping("addStart")
	public String addStart(String num,String ip,Model model) {
		attendanceService.addStart(num);
		model.addAttribute("ip",ip);
		return "welcome";
	}

	/**
	 * 下班签到
	 * 
	 * @param num
	 * @return
	 */
	@RequestMapping("addEnd")
	public String addEnd(String num) {
		attendanceService.addEnd(num);
		return "welcome";
	}

	// 考勤
	@RequestMapping("toAttendance")
	public String toAttendance(Model model) {
		List<Attendance> attendances = attendanceService.findAll();
		model.addAttribute("attendances", attendances);
		return "attendance_list";
	}

	// 加班页面
	@RequestMapping("overTime")
	public String overTime(Model model) {
		List<Overtime> overtimes = attendanceService.findAllOver();
		model.addAttribute("overtimes", overtimes);
		return "overtime_list";
	}

	// 加班修改页面
	@RequestMapping("toOverTimeUpdatepag")
	public String toOverTimeUpdatepag(String oid, String dname, String ename, String time, Model model) {
		Overtime overtime = attendanceService.findByid(Integer.parseInt(oid));
		model.addAttribute("dname", dname);
		model.addAttribute("ename", ename);
		model.addAttribute("overtime", overtime);
		model.addAttribute("time", time);
		return "overtime_update";
	}

	// 加班修改
	@RequestMapping("toOverTimeUpdate")
	public String toOverTimeUpdate(String eid, String date) throws ParseException {
		Overtime overtime = attendanceService.findByid(Integer.parseInt(eid));
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date newdate = format.parse(date);
		overtime.setDay(newdate);
		Integer updatetime = attendanceService.updatetime(overtime);
		return "forward:overTime";
	}

	@RequestMapping("del")
	@ResponseBody
	public Object todel(String id) {
		Integer delOver = attendanceService.delOver(Integer.parseInt(id));
		if (delOver != 1) {
			return 0;
		}
		return 1;
	}

	@RequestMapping("toAddTime")
	public String toAddTime() {
		return "overtime_add";

	}

	@RequestMapping("getDepData")
	@ResponseBody
	public Object getDepData() {
		List<Department> deps = attendanceService.findAllDep();
		return deps;

	}

	@RequestMapping("getEmpData")
	@ResponseBody
	public Object getEmpData(String departmentNumber) {
		List<Employee> emps = attendanceService.findbydep(Integer.parseInt(departmentNumber));
		return emps;

	}

	@RequestMapping("add")
	public String add(String departmentNumber, String employeeNumber, String date) throws ParseException {
		// System.out.println(departmentNumber+","+employeeNumber+","+date);
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date newdate = format.parse(date);
		attendanceService.addOverTime(Integer.parseInt(departmentNumber), Integer.parseInt(employeeNumber), newdate);
		return "forward:overTime";
	}
}
